<?php

/*
 *
 */

class Model_Mediate_ReturnedMoneyPlanDetail extends Model_BaseModel
{
    use Lib_BaseTraits;
    /**
     * 缓存前缀
     *
     * @var unknown
     */
    private static $tag = 'Model_Mediate_ReturnedMoneyPlanDetail';
    /**
     * Undocumented variable.
     *
     * @var string
     */
    private $table = 'returned_money_plan_detail';

    public function __construct()
    {
        // 选择连接的数据库
        parent::_init('mediation');
    }
    function getAllBySearch($fields = array(),$search = array(),$cache = false,$count=false){
        $fields = empty($fields) ? '*' : implode(',',$fields);
        if($count){
            $fields = ' count(*) as total ';
        }
        $search_str = '';
        if(isset($search['where']) && !empty($search['where'])){
            $search_str .= 'WHERE '.implode(' AND ',$search['where']);
        }
        if(isset($search['group']) && $search['group']){
            $search_str .= ' GROUP BY '.$search['group'];
            if(isset($search['having']) && $search['having']){
                $search_str .= ' HAVING '.$search['having'];
            }
        }
        if(isset($search['order']) && $search['order']){
            $search_str .= ' ORDER BY '.$search['order'];
        }
        if(!$count && isset($search['limit']) && $search['limit']){
            $search['start'] = isset($search['start']) ? $search['start'] : 0;
            $search_str .= ' LIMIT '.$search['start'].','.$search['limit'];
        }
        $join = empty($search['join']) ? '' : $search['join'];
        $sql = "SELECT {$fields} FROM {$this->table} {$join} {$search_str}";
        $cache ? $this->dao->conn()->setTag(self::$tag) : $this->dao->conn(false)->noCache();
        $res = $this->dao->preparedSql($sql, array())->fetchAll();
        return $res;
    }
    public function saveByIds($updateDta, $ids, $where = [], $pk)
    {
        $data = [];
        $sql = 'UPDATE `returned_money_plan_detail` SET ';
        $sqlarr = array();
        foreach ($updateDta as $key => $value) {
            array_push($sqlarr, "`{$key}`=:$key");
            $data[":$key"] = $value;
        }
        $where_str = '';
        if (!empty($where)) {
            $where_str = 'AND '.implode(' AND ', $where);
        }
        $sql .= implode(',', $sqlarr);
        $sql .= " WHERE {$pk} in ({$ids}) {$where_str}";
        $res = $this->dao->conn(false)->noCache()->preparedSql($sql, $data)->affectedCount();
        $this->dao->clearTag(self::$tag);
        return $res;
    }
}
